<template>
  <div style="padding:30px;">
    <el-alert
        title="统计图系"
        type="info"
        :closable="false"
        show-icon
        description="这里是一些统计图"
        effect="dark"/>
        <el-row :gutter="40" class="panel-group">
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel">
        <div class="card-panel-icon-wrapper icon-people">
          <svg-icon icon-class="user" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            Users
          </div>
          <count-to :start-val="0" :end-val="userNumber" :duration="2600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" >
        <div class="card-panel-icon-wrapper icon-message">
          <svg-icon icon-class="peoples" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            Passengers
          </div>
          <count-to :start-val="0" :end-val="passengerNumber" :duration="3000" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" >
        
          <div class="card-panel-icon-wrapper icon-shopping"><svg-icon icon-class="shopping" class-name="card-panel-icon" /></div>
          
        <div class="card-panel-description">
          <div class="card-panel-text">
            Purchases
          </div>
          <count-to :start-val="0" :end-val="purchase" :duration="3200" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" >
        <div class="card-panel-icon-wrapper icon-money"> <svg-icon icon-class="money" class-name="card-panel-icon" /></div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            Income
          </div>
          <count-to :start-val="0" :end-val="income" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
  </el-row>
    <el-card class="box-card" style="height: 280px; width:1280px;text-align: center;">
      <div style="height: 280px;" ref="linechart"></div>
    </el-card>
    <el-card style="width:500px">
      <div style="height: 350px" ref="rosechart"></div>
    </el-card>
  </div>
</template>
<script>
import * as echarts from "echarts"
import { queryAllOrder } from "@/api/order"
import {queryAllUser} from "@/api/user"
import {queryAllPassenger} from "@/api/table"
import moment from 'moment'
import CountTo from 'vue-count-to'
export default{
    name:"Mychart",
      components: {
        CountTo
    },
    data(){
        return{
          userNumber:0,
          passengerNumber:0,
          purchase:0,
          income:0,

        }
    },
    created(){

    },
    mounted(){
      queryAllOrder().then(response =>{
        this.handleLineChart(response.data.data)
        this.handleRoseChart(response.data.data)
        response.data.data.forEach(element=>{
          if(element.status=='已付款'){
            this.purchase++
            this.income=this.income+parseInt(element.price.substr(1,element.price.length))
          }
        })
      })
      queryAllUser().then(response =>{
        this.userNumber=response.data.data.length
      })
      queryAllPassenger().then(response =>{
        var contain=[]
        response.data.data.forEach(element=>{
          if(!contain.includes(element.card)){
            contain.push(element.card)
          }
        })
        this.passengerNumber=contain.length
      })
    },
    methods:{
        handleLineChart(allOrderInfo){
          console.log(allOrderInfo)
          var dayData=[]
          const realOrderData=[]
          const countData=[]
          const moneyData=[]
          const refundData=[]
          //只统计已付款的，其余都是假数据
          allOrderInfo.forEach((element,index) =>{
            if(element.status=='已付款'){
              realOrderData.push(element)
            }
          })
          //统计dayData，即有哪几天有交易
          realOrderData.forEach(element =>{
            if(!dayData.includes(element.create_time.substr(0,10))){
              dayData.push(element.create_time.substr(0,10))//获取日期
              countData.push(0)//初始化，数量
              moneyData.push(0)//初始化，金额
              refundData.push(0)
            }
          })
          //对日期进行时间排序
          dayData.sort((a, b) => moment(a).toDate() - moment(b).toDate())
          dayData.forEach((element,index)=>{
            realOrderData.forEach(item =>{
              if(item.create_time.substr(0,10)===element){
                countData[index]++
                moneyData[index]=moneyData[index]+parseInt(item.price.substr(1,item.price.length))
              }
            })
          })
          dayData.forEach((element,index)=>{
            allOrderInfo.forEach(item =>{
              if(item.create_time.substr(0,10)===element){
                if(item.status==='已退票'){
                  refundData[index]++
                }
              }
            })
          })
      //统计表格的样式配置    
    const option = {
      title:{
        text: '销售统计',
        left: 'left'
      },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },
        toolbox: {
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        legend: {
          data: ['售票数量','退票数量','收益金额']
        },
        xAxis: [
          {
            type: 'category',
            data: dayData,
            axisPointer: {
              type: 'shadow'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '数量',
            min: 0,
            max: 10,
            interval: 2,
            axisLabel: {
              formatter: '{value} 张'
            }
          },
          {
            type: 'value',
            name: '收益金额',
            min: 0,
            max: 3000,
            interval: 500,
            axisLabel: {
              formatter: '{value} 元'
            }
          }
        ],
        series: [
          {
            name: '售票数量',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value + ' 张';
              }
            },
            data:countData
          },
          {
            name: '退票数量',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value + ' 张';
              }
            },
            data:refundData
          },
          {
            name: '收益金额',
            type: 'line',
            yAxisIndex: 1,
            tooltip: {
              valueFormatter: function (value) {
                return value + ' 元';
              }
            },
            data: moneyData
          }
        ]
      };
      const e = echarts.init(this.$refs.linechart);
      e.setOption(option);
        },
        handleRoseChart(allOrderInfo){
          const cityData=[]
          const countData=[]
          //处理一下车站名字
          allOrderInfo.forEach(element =>{
            if(element.end_station_name.charAt(element.end_station_name.length-1)==='东'||element.end_station_name.charAt(element.end_station_name.length-1)==='北'||element.end_station_name.charAt(element.end_station_name.length-1)==='南'||element.end_station_name.charAt(element.end_station_name.length-1)==='西'){
              element.end_station_name=element.end_station_name.substr(0,element.end_station_name.length-1)
            }
          })
          //构造cityData
          allOrderInfo.forEach(element =>{
            if(!cityData.includes(element.end_station_name)){
              cityData.push(element.end_station_name)
              countData.push({name:element.end_station_name,value:1})//初始化countData
            }
            else{
              countData.forEach(item=>{
                if(item.name === element.end_station_name){
                  item.value++
                }
              })
            }
          })
          const option = {
          title: {
            text: '热门城市玫瑰图',
            left: 'left'
          },
          tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
          },
          legend: {
            left: 'center',
            top: 'bottom',
            data:cityData
          },
          toolbox: {
            show: true,
            feature: {
              mark: { show: true },
              dataView: { show: true, readOnly: false },
              restore: { show: true },
              saveAsImage: { show: true }
            }
          },
          series: [
            {
              name: 'Area Mode',
              type: 'pie',
              radius: [20, 140],
              center: ['50%', '50%'],
              roseType: 'area',
              itemStyle: {
                borderRadius: 5
              },
              data: countData
            }
          ]
        }
        const e = echarts.init(this.$refs.rosechart);
        e.setOption(option);
        }
    }

}
</script>
<style scoped lang="scss">
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-people {
        background: #40c9c6;
      }

      .icon-message {
        background: #36a3f7;
      }

      .icon-money {
        background: #f4516c;
      }

      .icon-shopping {
        background: #34bfa3
      }
    }

    .icon-people {
      color: #40c9c6;
    }

    .icon-message {
      color: #36a3f7;
    }

    .icon-money {
      color: #f4516c;
    }

    .icon-shopping {
      color: #34bfa3
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
.box-card {
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 20px;
  }
</style>